<template>
  <div>
    <el-form label-width="120px" :model="userdata">
      <el-form-item label="用户名">
        <el-input size="small" v-model="userdata.userName" auto-complete="off" placeholder="请输入用户名称"></el-input>
      </el-form-item>
      <el-form-item label="用户昵称">
        <el-input size="small" v-model="userdata.nickName" auto-complete="off" placeholder="请输入用户昵称码"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input size="small" v-model="userdata.phonenumber" auto-complete="off" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input size="small" v-model="userdata.email" auto-complete="off" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <!--
       action 后端接收文件上传的路径
       name  发送到后端的key
       -->
      <el-form-item label="头像">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:8888/api/sysUser/tx/upload"
          :show-file-list="false"
          name="img"
          :data="userId"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" class="title" @click="submitForm()">保存
      </el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: "UserInfo",
  data() {
    return {
      userdata: {},
      imageUrl: '',
      userId:null
    }
  },
  created() {
    // 从本地缓存中取出数据赋值给变量,用于回显
    this.userdata = JSON.parse(localStorage.getItem('userdata'))
    this.userId = {userId:this.userdata.userId};
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.userdata.avatar = this.imageUrl;
      localStorage.setItem('userdata',this.userdata)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    submitForm(){
      this.$router.push("/index/group/user")
    }
  }
}
//#d9d9d9
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
